package com.person.ntv.service.base.impl;

import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.RowBounds; 
import com.person.exception.ServiceException;
import com.person.ntv.baseMapper.BaseMapper;
import com.person.ntv.service.base.IBaseService;
 
/**@Title: BaseServiceImpl
 * @Description: https://www.oschina.net/p/mybatis-mapper 通用Mapper地址
 * @author lqy
 * @date 2016年11月16日
 * @version V1.0
 */
public  class  BaseServiceImpl<T> implements IBaseService<T>{
    private BaseMapper<T> baseMapper;
    public void setBaseMapper(BaseMapper<T> mapper){
    	this.baseMapper=mapper;
    }
	@Override
	public int delete(T t) throws ServiceException {
		try{
		//根据实体属性作为条件进行删除，查询条件使用等号
		return baseMapper.delete(t);
		}catch(Exception e){
			e.printStackTrace();
			throw new ServiceException("数据库操作错误！");
		}
	}

	@Override
	public int deleteByExample(Object o) throws ServiceException {
		//根据Example条件删除数据
		try{
		return baseMapper.deleteByExample(o);
	       }catch(Exception e){
		e.printStackTrace();
		throw new ServiceException("数据库操作错误！");
	}
	}

	@Override
	public int deleteByPrimaryKey(Object o) throws ServiceException {
		//根据主键字段进行删除，方法参数必须包含完整的主键属性
		try{
		return baseMapper.deleteByPrimaryKey(o);
		}catch(Exception e){
			e.printStackTrace();
			throw new ServiceException("数据库操作错误！");
		}
	}

	@Override
	public int insert(T t) throws ServiceException {
		//保存一个实体，null的属性也会保存，不会使用数据库默认值
		try{
		return baseMapper.insert(t);
		}catch(Exception e){
			e.printStackTrace();
			throw new ServiceException("数据库操作错误！");
		}
	}

	@Override
	public int insertSelective(T t) throws ServiceException {
		//保存一个实体，null的属性不会保存，会使用数据库默认值
		try{
		return baseMapper.insertSelective(t);
		}catch(Exception e){
			e.printStackTrace();
			throw new ServiceException("数据库操作错误！");
		}
	}
	
	@Override
	public List<T> select(T t) throws ServiceException {
		//根据实体中的属性值进行查询，查询条件使用等号
		try{
		return baseMapper.select(t);
		}catch(Exception e){
			e.printStackTrace();
			throw new ServiceException("数据库操作错误！");
		}
	}

	@Override
	public List<T> selectAll() throws ServiceException {
		//查询全部结果，select(null)方法能达到同样的效果
		try{
		return baseMapper.selectAll();
		}catch(Exception e){
			e.printStackTrace();
			throw new ServiceException("数据库操作错误！");
		}
	}

	@Override
	public List<T> selectByExample(Object o) throws ServiceException {
		//说明：根据Example条件进行查询
		//重点：这个查询支持通过Example类指定查询列，通过selectProperties方法指定查询列
		try{
		return baseMapper.selectByExample(o);
		}catch(Exception e){
			e.printStackTrace();
			throw new ServiceException("数据库操作错误！");
		}
	}

	@Override
	public List<T> selectByExampleAndRowBounds(Object o, RowBounds rb)
			throws ServiceException {
		//根据example条件和RowBounds进行分页查询
		try{
		return baseMapper.selectByExampleAndRowBounds(o, rb);
		}catch(Exception e){
			e.printStackTrace();
			throw new ServiceException("数据库操作错误！");
		}
	}

	@Override
	public T selectByPrimaryKey(Object o) throws ServiceException {
		//根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
		try{
		return baseMapper.selectByPrimaryKey(o);
		}catch(Exception e){
			e.printStackTrace();
			throw new ServiceException("数据库操作错误！");
		}
	}

	@Override
	public List<T> selectByRowBounds(T t, RowBounds rb) throws ServiceException {
		//根据实体属性和RowBounds进行分页查询
		try{
		return baseMapper.selectByRowBounds(t, rb);
		}catch(Exception e){
			e.printStackTrace();
			throw new ServiceException("数据库操作错误！");
		}
	}

	@Override
	public int selectCount(T t) throws ServiceException {
		//根据实体中的属性查询总数，查询条件使用等号
		try{
		return baseMapper.selectCount(t);
		}catch(Exception e){
			e.printStackTrace();
			throw new ServiceException("数据库操作错误！");
		}
	}

	@Override
	public int selectCountByExample(Object o) throws ServiceException {
		//根据Example条件进行查询总数
		try{
		return baseMapper.selectCountByExample(o);
		}catch(Exception e){
			e.printStackTrace();
			throw new ServiceException("数据库操作错误！");
		}
	}

	@Override
	public T selectOne(T t) throws ServiceException {
		//根据实体中的属性进行查询，只能有一个返回值，有多个结果是抛出异常，查询条件使用等号
		try{
		return baseMapper.selectOne(t);
		}catch(Exception e){
			e.printStackTrace();
			throw new ServiceException("数据库操作错误！");
		}
	}

	@Override
	public int updateByExample(@Param("record") T record, @Param("example") Object example) throws ServiceException {
		//根据Example条件更新实体record包含的全部属性，null值会被更新
		try{
		return baseMapper.updateByExample(record,example);
		}catch(Exception e){
			e.printStackTrace();
			throw new ServiceException("数据库操作错误！");
		}
	}

	@Override
	public int updateByExmapleSelective(@Param("record") T record, @Param("example") Object example) throws ServiceException {
		//根据Example条件更新实体record包含的不是null的属性值
		try{
		return baseMapper.updateByExampleSelective(record,example);
		}catch(Exception e){
			e.printStackTrace();
			throw new ServiceException("数据库操作错误！");
		}
	}

	@Override
	public int updateByPrimaryKey(T t) throws ServiceException {
		//根据主键更新实体全部字段，null值会被更新
		try{
		return baseMapper.updateByPrimaryKey(t);
		}catch(Exception e){
			e.printStackTrace();
			throw new ServiceException("数据库操作错误！");
		}
	}

	@Override
	public int updateByPrimaryKeySelective(T t) throws ServiceException {
		//根据主键更新属性不为null的值
		try{
		return baseMapper.updateByPrimaryKeySelective(t);
		}catch(Exception e){
			e.printStackTrace();
			throw new ServiceException("数据库操作错误！");
		}
	}
	
	@Override
	public int deleteByIds(String ids) throws ServiceException {
		try{
			return baseMapper.deleteByIds(ids);
			}catch(Exception e){
				e.printStackTrace();
				throw new ServiceException("数据库操作错误！");
			}
	}
	
	@Override
	public List<T> selectByIds(String ids) throws ServiceException {
		try{
			return baseMapper.selectByIds(ids);
			}catch(Exception e){
				e.printStackTrace();
				throw new ServiceException("数据库操作错误！");
			}
	}
	@Override
	public int insertList(List<T> recordList) throws ServiceException {
		try{
			return baseMapper.insertList(recordList);
			}catch(Exception e){
				e.printStackTrace();
				throw new ServiceException("数据库操作错误！");
			}
	}
	@Override
	public int insertUseGeneratedKeys(T record) throws ServiceException {
		try{
			return baseMapper.insertUseGeneratedKeys(record);
			}catch(Exception e){
				e.printStackTrace();
				throw new ServiceException("数据库操作错误！");
			}
	}
}
